﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GraphicSplit101
{
    public partial class UCVarianceTolerance : UserControl
    {
        private Bitmap bitmap = GraphicSplit101.Properties.Resources.Sample;

        public Bitmap Bitmap
        {
            get { return bitmap; }
            set { bitmap = value; }
        }

        private int pid = 1;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }

        public UCVarianceTolerance()
        {
            InitializeComponent();

            pictureBox1.Image = bitmap;

            dataGridView1.RowCount = 8;

            int count = Convert.ToInt32(DBUtils.GetUniqueValue("select count(1) from InspectionPreAreaDetail where [pid]=" + pid));

            if (count == 0)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 8; i++)
                {
                    sb.Append("insert into InspectionPreAreaDetail([pid], Co1,Co2,Co3,Co4,Co5,Co6,Co7,Co8, [sort]) "
                        + " values(" + Pid + ", 1, 1, 1, 1, 1, 1, 1, 1, " + (i + 1) + ")\n");
                }
                DBUtils.ExecuteSql(sb.ToString());
            }
            DataTable dt = DBUtils.GetDataTable("select Co1,Co2,Co3,Co4,Co5,Co6,Co7,Co8 from InspectionPreAreaDetail where [pid]=" + pid + " order by [Sort]");
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int cellWidth = pictureBox1.Width / 8;
            int cellHeight = pictureBox1.Height / 8;

            int startX = cellWidth * e.ColumnIndex;
            int startY = cellHeight * e.RowIndex;

            Graphics g = pictureBox1.CreateGraphics();
            pictureBox1.Refresh();

            g.DrawRectangle(new Pen(Color.Red), startX, startY, cellWidth, cellHeight);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 8; i++)
            {
                sb.Append("update InspectionPreAreaDetail set "
                    + "Co1=" + dataGridView1.Rows[i].Cells[0].Value + ","
                    + "Co2=" + dataGridView1.Rows[i].Cells[1].Value + ","
                    + "Co3=" + dataGridView1.Rows[i].Cells[2].Value + ","
                    + "Co4=" + dataGridView1.Rows[i].Cells[3].Value + ","
                    + "Co5=" + dataGridView1.Rows[i].Cells[4].Value + ","
                    + "Co6=" + dataGridView1.Rows[i].Cells[5].Value + ","
                    + "Co7=" + dataGridView1.Rows[i].Cells[6].Value + ","
                    + "Co8=" + dataGridView1.Rows[i].Cells[7].Value
                    + " where [pid]=" + Pid + " and [sort]=" + (i + 1) + "\n");
            }
            DBUtils.ExecuteSql(sb.ToString());
            MessageBox.Show("保存成功！");
        }
    }
}
